package com.lib.view.progressbar;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ProgressBar;
import android.widget.SeekBar;

import com.lib.fastkit.utils.log.LogUtil;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import androidx.appcompat.widget.AppCompatSeekBar;

/**
 * 视频及问题进度条
 * Created by Administrator on 2020/5/14 0014.
 */

public class ProgressBar1 extends AppCompatSeekBar {

    /**
     * 未选中时的连线画笔
     */
    private Paint mLinePaint;
    /**
     * 选中时的连线画笔
     */
    private Paint mLineSelectedPaint;

    /**
     * 未选中时的实心圆画笔
     */
    private Paint mCirclePaint;

    /**
     * 未选中时的外部边框圆画笔
     */
    private Paint mCircleStrokePaint;
    /**
     * 选中时的内部实心圆画笔
     */
    private Paint mCircleSelPaint;
    /**
     * 选中时的边框圆画笔
     */
    private Paint mCircleStrokeSelPaint;

    /**
     * 进度条未选中的颜色
     */
    private int mLineNormal = Color.parseColor("#989898");

    /**
     * 进度条选中时的颜色
     */
    private int mLineSelected = Color.parseColor("#ffffff");

    /**
     * 未选中时圆的画笔b3b3b3
     */
    private int mColorNormal = Color.parseColor("#989898");

    /**
     * 未选中时圆外部边框的画笔
     */
    private int mColorStrokeNormal = Color.parseColor("#000000");

    /**
     * 选中时内心圆的颜色
     */
    private int mColorSelected = Color.parseColor("#68c4ff");

    /**
     * 选中时边框圆的颜色
     */
    private int mColorSelectedStroke = Color.parseColor("#ffa9b1");

    /**
     * 连线的高度
     */
    float mLineHeight = 3f;

    //圆的直径
    float mCircleHeight = 10f;
    float mCircleSelStroke = 3f;
    float mCircleFillRadius = 5f;

    int defaultHeight;

    /**
     * 节点底部的文字列表
     */
    List<String> textList = new ArrayList<>();

    /**
     * 文字同宽高的矩形，用来测量文字
     */
    List<Rect> mBounds;

    /**
     * 存储每个圆心在同一直线上的节点圆的 x 坐标值
     */
    List<Float> circleLineJunctions = new ArrayList<>();

    /**
     * 选中项集合
     */
    List<Integer> selectedIndexSet = new ArrayList<>();

    public ProgressBar1(Context context) {
        super(context);
    }

    public ProgressBar1(Context context, AttributeSet attrs) {
        super(context, attrs);
        initPaint();
    }

    public ProgressBar1(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * 初始化画笔属性
     */
    private void initPaint() {

        mLinePaint = new Paint();
        mLineSelectedPaint = new Paint();
        mCirclePaint = new Paint();
        mCircleStrokeSelPaint = new Paint();
        mCircleSelPaint = new Paint();

        mCircleStrokePaint = new Paint();

        mLinePaint.setColor(mLineNormal);
        //设置填充
        mLinePaint.setStyle(Paint.Style.FILL);
        //笔宽像素
        mLinePaint.setStrokeWidth(mLineHeight);
        //锯齿不显示
        mLinePaint.setAntiAlias(true);

        mLineSelectedPaint.setColor(mLineSelected);
        mLineSelectedPaint.setStyle(Paint.Style.FILL);
        mLineSelectedPaint.setStrokeWidth(mLineHeight);
        mLineSelectedPaint.setAntiAlias(true);

        //未选中时画笔
        mCirclePaint.setColor(mColorNormal);
        //设置填充
        mCirclePaint.setStyle(Paint.Style.FILL);
        //笔宽像素
        mCirclePaint.setStrokeWidth(15);
        //锯齿不显示
        mCirclePaint.setAntiAlias(true);

        //未选中时外部边框圆画笔
        mCircleStrokePaint.setColor(mColorStrokeNormal);
        //设置填充
        mCircleStrokePaint.setStyle(Paint.Style.FILL);
        //笔宽像素
        mCircleStrokePaint.setStrokeWidth(mCircleSelStroke);
        //锯齿不显示
        mCircleStrokePaint.setAntiAlias(true);

        //选中时外框空心圆圈画笔
        mCircleStrokeSelPaint.setColor(mColorSelectedStroke);
        mCircleStrokeSelPaint.setStyle(Paint.Style.FILL);
        mCircleStrokeSelPaint.setStrokeWidth(mCircleSelStroke);
        mCircleStrokeSelPaint.setAntiAlias(true);

        //选中时的内部填充圆画笔
        mCircleSelPaint.setStyle(Paint.Style.FILL);
        mCircleSelPaint.setStrokeWidth(10);
        mCircleSelPaint.setAntiAlias(true);
        mCircleSelPaint.setColor(mColorSelected);

    }

    /**
     * 测量文字的长宽，将文字视为rect矩形
     */
    private void measureText() {
        mBounds = new ArrayList<>();
        for (String name : textList) {
            Rect mBound = new Rect();
            mBounds.add(mBound);
        }
    }


    /**
     * 测量view的高度
     */
    private void measureHeight() {
        if (mBounds != null && mBounds.size() != 0) {
            defaultHeight = (int) (mCircleHeight + mCircleSelStroke + mBounds.get(0).height() / 2);
        } else {
            defaultHeight = (int) (mCircleHeight + mCircleSelStroke + mCircleFillRadius);
        }
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
        //宽高都设置为wrap_content
        if (widthSpecMode == MeasureSpec.AT_MOST && heightSpecMode == MeasureSpec.AT_MOST) {
            //宽设置为wrap_content
            setMeasuredDimension(widthSpecSize, defaultHeight);
        } else if (widthSpecMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(widthSpecSize, heightSpecSize);
        } else if (heightSpecMode == MeasureSpec.AT_MOST) {
            //高设置为wrap_content
            setMeasuredDimension(widthSpecSize, defaultHeight);
        } else {
            //宽高都设置为match_parent或具体的dp值
            setMeasuredDimension(widthSpecSize, heightSpecSize);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //若未设置节点标题或者选中项的列表，则取消绘制
        if (selectedIndexSet == null || selectedIndexSet.isEmpty() ||
                mBounds == null || mBounds.isEmpty()) {
            return;
        }
        //绘制文字和圆形
        for (int i = 0; i < selectedIndexSet.size(); i++) {
            float cx;
            float cy;
            //中间部分的节点
            cx = mCircleHeight / 2 + selectedIndexSet.get(i);
            cy = mCircleHeight / 2 + mCircleSelStroke;
            circleLineJunctions.add(cx - mCircleHeight / 2);
            circleLineJunctions.add(cx + mCircleHeight / 2);

        }

        for (int i = 0, j = 1; j <= circleLineJunctions.size() && !circleLineJunctions.isEmpty(); ++i, j = j + 2) {
            if (getSelectedIndexSet().contains(i)) {
                canvas.drawLine(0, mCircleHeight / 2 + mCircleSelStroke,
                        circleLineJunctions.get(j) + 200, mCircleHeight / 2 + mCircleSelStroke, mLineSelectedPaint);
            } else {
                canvas.drawLine(0, mCircleHeight / 2 + mCircleSelStroke,
                        circleLineJunctions.get(j) + 200, mCircleHeight / 2 + mCircleSelStroke, mLinePaint);
            }

            if (getSelectedIndexSet().contains(i)) {
                if (i<selectedIndexSet.size()){
                    //若当前位置节点被包含在选中项Set中，判定此节点被选中
                    canvas.drawCircle(mCircleHeight / 2 + selectedIndexSet.get(i), mCircleHeight / 2 + mCircleSelStroke, mCircleHeight, mCircleStrokeSelPaint);
                    canvas.drawCircle(mCircleHeight / 2 + selectedIndexSet.get(i), mCircleHeight / 2 + mCircleSelStroke, mCircleFillRadius, mCircleSelPaint);
                }

            } else {
                if (i<selectedIndexSet.size()){
                //若当前位置节点没有被包含在选中项Set中，判定此节点没有被选中
                canvas.drawCircle(mCircleHeight / 2 + selectedIndexSet.get(i), mCircleHeight / 2 + mCircleSelStroke, mCircleHeight, mCircleStrokePaint);
                canvas.drawCircle(mCircleHeight / 2 + selectedIndexSet.get(i), mCircleHeight / 2 + mCircleSelStroke, mCircleFillRadius, mCirclePaint);
                }
            }
        }

    }

    /**
     * 供外部调用，显示控件
     *
     * @param titles   底部标题内容列表
     * @param indexSet 选中项Set
     */
    public void show(List<String> titles, List<Integer> indexSet) {
        if (titles != null && !titles.isEmpty()) {
            this.textList = titles;
        }
        if (indexSet != null && !indexSet.isEmpty()) {
            this.selectedIndexSet = indexSet;
        }
        measureText();
        measureHeight();
        //绘制
        invalidate();
    }

    /**
     * 更新底部节点标题内容
     *
     * @param textList 节点标题内容列表
     */
    public void refreshTextList(List<String> textList) {
        this.textList = textList;
        measureText();
        measureHeight();
        invalidate();
    }

    /**
     * 获取节点选中状态
     *
     * @return 节点选中状态列表
     */
    public List<Integer> getSelectedIndexSet() {
        return selectedIndexSet;
    }

    /**
     * 更新选中项
     *
     * @param set 选中项Set
     */
    public void refreshSelectedIndexSet(List<Integer> set) {
        this.selectedIndexSet = set;
        invalidate();
    }

    // 实现节点切换，把注释打开就行了
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float eventX;
        float eventY;
        int i = event.getAction();
        if (i == MotionEvent.ACTION_MOVE) {
        } else if (i == MotionEvent.ACTION_DOWN) {

            eventX = event.getX();
            eventY = event.getY();

            LogUtil.e("eventX==" + eventX + "eventY==" + eventY);

        } else if (i == MotionEvent.ACTION_UP) {
            eventX = event.getX();
            eventY = event.getY();
            //计算选中的index
            float select = eventX;
            float xs = select - (int) (select);
//            selectIndex = (int) select + (xs > 0.5 ? 1 : 0);

            LogUtil.e("eventX==" + eventX + "eventY==" + eventY + "select==" + select);
        }
        invalidate();
        return super.onTouchEvent(event);
    }


}
